﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc.Html;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace Fashion.Helpers
{
    public static class HtmlHelpers
    {
        public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {

            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var sb = new StringBuilder();

            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                sb.Append("<li class=\"active\">");
            else
                sb.Append("<li>");

            sb.Append(helper.ActionLink(linkText, actionName, controllerName));
            sb.Append("</li>");
            return sb.ToString();

        }

        public static string AdminItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {

            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var sb = new StringBuilder();

            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                sb.Append("<li class=\"current\">");
            else
                sb.Append("<li>");

            sb.Append(helper.ActionLink(linkText, actionName, controllerName));
            sb.Append("</li>");
            return sb.ToString();

        }

        public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
               Expression<Func<TModel, TProperty>> expression,
               object htmlAttributes)
            {
                IDictionary<string, string> attribHash = htmlAttributes as IDictionary<string, string>;
                if (attribHash == null)
                    return helper.LabelFor(expression);
     
                string displayName =  ModelMetadata.FromLambdaExpression(
                    expression, helper.ViewData).DisplayName;
     
                string attributes = string.Empty;
                foreach (var kp in attribHash)
                {
                    attributes += string.Format(" {0}=\"{1}\"", kp.Key, kp.Value);
                }
     
                return MvcHtmlString.Create(
                    string.Format("<label for=\"{0}\" {1}>{0}</label>", displayName, attributes.Trim()));
            }
        }

}